//
//  HDIdentityTipsView.swift
//  HuaDaoPeifu
//
//  Created by ZERO on 2018/6/15.
//  Copyright © 2018年 paiyipai. All rights reserved.
//

import UIKit

class HDIdentityTipsView: UIView {

    var shadowView : UIView?
    fileprivate var completionHandler:(()->())?

    @IBOutlet weak var tipiconImgV: UIImageView!
    @IBOutlet weak var lbMsg: UILabel!
    
    static func showSuccessTip(showMessage msg : NSAttributedString!,_ completionHandler:@escaping ()->()){
        
        let view = UINib(nibName: "HDIdentityTipsView", bundle: nil).instantiate(withOwner: self, options: nil).last as! HDIdentityTipsView
        view.lbMsg.attributedText = msg;
        view.lbMsg.textAlignment = NSTextAlignment.left
        view.tipiconImgV.image = UIImage(named: "success")
        view.completionHandler = completionHandler
        
        view.layer.setAffineTransform(CGAffineTransform(scaleX:1.1,y: 1.1))
        
        UIView.animate(withDuration: 0.15, delay:0.01, options: [], animations: {
            ()-> Void in
            view.layer.setAffineTransform(CGAffineTransform(scaleX:1/1.1,y: 1/1.1))
        },completion:{
            (finished:Bool) -> Void in
            
            
        })
        
        
        view.shadowView =  UIView()
        view.shadowView?.backgroundColor = UIColor.black
        view.shadowView?.alpha = 0.3
        view.shadowView?.frame = UIScreen.main.bounds
        
        let tapGesture = UITapGestureRecognizer(target: view,action: #selector(HDIdentityTipsView.dismissAction(_:)))
        view.shadowView?.addGestureRecognizer(tapGesture)
        if (view.superview == nil) {
            UIApplication.shared.keyWindow?.addSubview(view.shadowView!)
            UIApplication.shared.keyWindow?.addSubview(view)
        }
        view.snp.makeConstraints { (make) in
            make.width.equalTo(AppConstant.screenWidth - 30 * 2)
            make.height.equalTo(300)
            make.center.equalToSuperview()
        }
        
        view.layer.cornerRadius = 5
        view.layer.masksToBounds = true
        
        view.showwindow()
    }
    static func initHDIdentityTipsView(showMessage msg : String!){
        
        var view = HDIdentityTipsView()
            view = UINib(nibName: "HDIdentityTipsView", bundle: nil).instantiate(withOwner: self, options: nil).last as! HDIdentityTipsView
        view.lbMsg.text = msg;
        view.layer.setAffineTransform(CGAffineTransform(scaleX:1.1,y: 1.1))
        
        UIView.animate(withDuration: 0.15, delay:0.01, options: [], animations: {
            ()-> Void in
//            view.layer.setAffineTransform(CGAffineTransform(scaleX:1.3,y: 1.3))
             view.layer.setAffineTransform(CGAffineTransform(scaleX:1/1.1,y: 1/1.1))
        },completion:{
            (finished:Bool) -> Void in
          
//                view.layer.setAffineTransform(CGAffineTransform(scaleX:1/1.3,y: 1/1.3))

            
        })
        
       
        view.shadowView =  UIView()
        view.shadowView?.backgroundColor = UIColor.black
        view.shadowView?.alpha = 0.3
        view.shadowView?.frame = UIScreen.main.bounds
        
        let tapGesture = UITapGestureRecognizer(target: view,action: #selector(HDIdentityTipsView.dismissAction(_:)))
        view.shadowView?.addGestureRecognizer(tapGesture)
        if (view.superview == nil) {
            UIApplication.shared.keyWindow?.addSubview(view.shadowView!)
            UIApplication.shared.keyWindow?.addSubview(view)
        }
        view.snp.makeConstraints { (make) in
            make.width.equalTo(AppConstant.screenWidth - 30 * 2)
            make.height.equalTo(220)
            make.center.equalToSuperview()
        }

        view.layer.cornerRadius = 5
        view.layer.masksToBounds = true

        view.showwindow()
        
    }
    
    func showwindow(){
        if (self.superview == nil) {
            UIApplication.shared.keyWindow?.addSubview(self.shadowView!)
            UIApplication.shared.keyWindow?.addSubview(self)
        }
    }
    
    
    
    
    func dismissAction(_ gesture:UITapGestureRecognizer){
        UIView.animate(withDuration: 0.3, animations: {
            self.shadowView?.alpha = 0.3
            
        }) { (BOOL) in
            self.shadowView?.removeFromSuperview()
            
        }
        self.removeFromSuperview()
        
        if let completionHandler = completionHandler {
            completionHandler()
        }
    }
    
    @IBAction func confirmAction(_ sender: UIButton) {
        
        dismissAction(UITapGestureRecognizer.init())
    }

    
    /*
    // Only override draw() if you perform custom drawing.
    // An empty implementation adversely affects performance during animation.
    override func draw(_ rect: CGRect) {
        // Drawing code
    }
    */

}
